/*
 * @Author: your name
 * @Date: 2020-09-18 11:36:06
 * @LastEditTime: 2021-01-17 21:47:20
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \egg-RESTfulAPI\app\controller\virtualEtc.js
 */
/*
 * @Author: your name
 * @Date: 2020-09-18 11:36:06
 * @LastEditTime: 2020-09-18 15:18:21
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \egg-RESTfulAPI\app\controller\virtualEtc.js
 */
const Controller = require('egg').Controller
let moment = require('moment');
const { v4 } = require('uuid')
class ETCController extends Controller {
    constructor(ctx) {
        super(ctx)


        this.getEtcStationsRule = {
            sceneId: { type: 'string', required: true, allowEmpty: false }
        }
        this.addEtcStationRule = {
            sceneId: { type: 'string', required: true, allowEmpty: false },
            roadId: { type: 'string', required: true, allowEmpty: false },
            lon: { type: 'number', required: true, allowEmpty: false },
            lat: { type: 'number', required: true, allowEmpty: false },
            stationName1: { type: 'string', required: true, allowEmpty: false },
            stationName2: { type: 'string', required: true, allowEmpty: false },
            stationType: { type: 'int', required: true, allowEmpty: false },
        }
        this.updateEtcStationRule = {
            _id: { type: 'string', required: true, allowEmpty: false },
            lon: { type: 'number', required: true, allowEmpty: false },
            lat: { type: 'number', required: true, allowEmpty: false },
            stationName1: { type: 'string', required: true, allowEmpty: false },
            stationName2: { type: 'string', required: true, allowEmpty: false },
            stationType: { type: 'int', required: true, allowEmpty: false },
        }

        this.removeEtcStationRule = {
            id: { type: 'string', required: true, allowEmpty: false },
        }
        this.getEtcStationRule = {
            id: { type: 'string', required: true, allowEmpty: false },
        }


    }


    async getEtcStations() {
        const { ctx } = this
        ctx.validate(this.getEtcStationsRule)

        let { sceneId } = ctx.request.body
        let sceneEtcStationModel = await this.ctx.model.SceneEtcStationModel.find({ sceneId: sceneId })

        ctx.helper.success({ ctx, res: { data: sceneEtcStationModel } })
    }

    async updateEtcStation() {
        const { ctx } = this
        ctx.validate(this.updateEtcStationRule)

        let { _id, roadId, lon, lat, stationName1, stationName2, stationType } = ctx.request.body
        let sceneEtcStationModel = await this.ctx.model.SceneEtcStationModel.findOneAndUpdate(
            {
                '_id': _id
            },
            {
                roadId, lon, lat, stationName1, stationName2, stationType
            },
            { upsert: true, new: true }
        )



        ctx.helper.success({ ctx, res: { sceneEtcStationModel: sceneEtcStationModel } })
    }

    async removeEtcStation() {
        const { ctx } = this
        ctx.validate(this.removeEtcStationRule)

        let { id } = ctx.request.body
        let sceneEtcStationModel = await this.ctx.model.SceneEtcStationModel.remove(
            {
                '_id': id
            },
        )
        ctx.helper.success({ ctx, res: { sceneEtcStationModel } })
    }

    async getEtcStation() {
        const { ctx } = this
        ctx.validate(this.getEtcStationRule)

        let { id } = ctx.request.body
        let sceneEtcStationModel = await this.ctx.model.SceneEtcStationModel.find({ _id: id })

        ctx.helper.success({ ctx, res: { sceneEtcStationModel: sceneEtcStationModel } })
    }


    async addEtcStation() {
        const { ctx } = this
        ctx.validate(this.addEtcStationRule)
        let { sceneId, roadId, lon, lat, stationName1, stationName2, stationType } = ctx.request.body
        let scene = await this.ctx.model.Scene.findOne({ _id: sceneId })
        if (!scene) {
            ctx.helper.InvaidRequest({ ctx, msg: "sceneId 异常" })
            return
        }



        let etcStation = await this.ctx.model.SceneEtcStationModel.create(

            {
                sceneId, roadId, lon, lat,
                stationName1, stationName2, stationType,
            }

        )

        etcStation = await this.ctx.model.SceneEtcStationModel.findOne({ _id: etcStation._id })
        ctx.helper.success({ ctx, res: { data: etcStation } })
    }








}


module.exports = ETCController
